//
//  TrophyGoddessCell.swift
//  btvideo
//
//  Created by mac on 2021/7/8.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes
import SwiftyJSON

class ST_TrophyNormalCell: UICollectionViewCell {
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .white
        [number_label,nickname_label,avatar_image_view,widget,line].forEach{ contentView.addSubview($0) }
        
        number_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
        }
        
        avatar_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(33)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(48)
        }
        
        nickname_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(91)
            make.right.equalToSuperview().offset(-9)
            make.top.equalToSuperview().offset(17)
            make.height.equalTo(20)
        }
        
        widget.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(91)
            make.bottom.equalToSuperview().offset(-17)
            make.height.equalTo(21)
        }
        
        line.snp.makeConstraints { (make) in
            make.right.bottom.equalToSuperview()
            make.height.equalTo(1)
            make.left.equalToSuperview().offset(33)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var number_label:UILabel = {
        let control = UILabel()
        control.text = ""
        control.textColor = #colorLiteral(red: 0.6745098039, green: 0.7058823529, blue: 0.7529411765, alpha: 1)
        control.font = UIFont.systemFont(ofSize: 13, weight: .medium)
        return control
    }()
    
    fileprivate lazy var nickname_label:UILabel = {
        let control = UILabel()
        control.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        control.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        return control
    }()
    

    
    fileprivate lazy var avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 48/2.0
        control.borderWidth = 2
        control.borderColor = .white
        let tap = UITapGestureRecognizer(target: self, action: #selector(click(gez:)))
        control.isUserInteractionEnabled = true
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var widget = ST_TrophyValueWidget()
    
    fileprivate lazy var line:UIView = {
       let control = UIView()
        control.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9725490196, blue: 0.9803921569, alpha: 1)
        return control
    }()
    
    weak var data:ST_TrophyNormalCellData?
    
}

extension ST_TrophyNormalCell{
    
    func bind(data:ST_TrophyNormalCellData,index:Int){
        
        self.data = data
        
        number_label.text = (index+4).string
        
        avatar_image_view.pin_setImage(from: data.user.avatar.compress_small_image(width: 48*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        nickname_label.text = data.user.nickname
        
        
        widget.backgroundColor = data.currencyBackgroundColor
        
        widget.title = data.currency
        
    }
    
    @objc fileprivate func click(gez:UITapGestureRecognizer){
        
        guard let d =  data else { return  }
        
        if d.user.rankClose == 2{
            self.parentViewController?.toast(text: "对方匿名，不能查看个人主页")
            return
        }
 
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = d.user.accid
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
        
    }
    
}



//MARK: - Model

class ST_TrophyNormalCellData{

    var createTime : String
    var currency : NSAttributedString
    var currencyBackgroundColor:UIColor?
    var day : String
    var gender : Int
    var id : Int
    var memberId : Int
    var user : ST_TrophyNormalCellUser

    init(fromJson json: JSON,rankType:ST_TrophyRankType){
        
        createTime = json["create_time"].stringValue
        day = json["day"].stringValue
        gender = json["gender"].intValue
        id = json["id"].intValue
        memberId = json["member_id"].intValue
        user = ST_TrophyNormalCellUser(fromJson: json["user"])
        
        var str = String(format: "%.2f", json["currency"].floatValue)
        switch rankType {
        case .财富:
            str = "富豪值：" + str
        case .魅力:
            str = "魅力值：" + str
        default: break
        }
        
        currency = str.withAttributes([Attribute.font(.systemFont(ofSize: 12)),.textColor(rankType.widgetColor)])
        
        currencyBackgroundColor = UIColor(hexString: rankType.widgetColor.hexString, transparency: 0.1)
        
    }
}


class ST_TrophyNormalCellUser{

    var accid : String
    var avatar : String
    var id : Int
    var nickname : String
    var rankClose : Int

    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON){
        accid = json["accid"].stringValue
        avatar = json["avatar"].stringValue
        id = json["id"].intValue
        nickname = json["nickname"].stringValue
        rankClose = json["rank_close"].intValue
    }
}
